unit SplashTestU1;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  ComCtrls, StdCtrls, ImgList, ActnList, Menus, ExtCtrls, JvProgressBar, Buttons;

const
  UM_ACTIVATE = WM_APP + 123;
type

  TForm1 = class(TForm)
    AboutButton: TButton;
    Image1: TImage;
    ListBox1: TListBox;
    btnLoesung: TBitBtn;
    btnItemUp: TBitBtn;
    btnItemDown: TBitBtn;
    btnZufall: TBitBtn;
    memInfo: TMemo;
    StatusBar1: TStatusBar;
    btnReset: TBitBtn;
    Panel1: TPanel;
    JvGradientProgressBar1: TJvGradientProgressBar;
    btnMemoLeeren: TBitBtn;
    btnBeenden: TBitBtn;
    Panel2: TPanel;
    lblTrefferCount: TLabel;
    lblInfoTreffer: TLabel;
    Panel3: TPanel;
    lblInsgesamtCount: TLabel;
    lblInfoGesamt: TLabel;
    Timer1: TTimer;
    MainMenu1: TMainMenu;
    N1: TMenuItem;
    kj1: TMenuItem;
    Zufall1: TMenuItem;
    Lsen1: TMenuItem;
    N2: TMenuItem;
    Programmbeenden1: TMenuItem;
    Giraffe1: TMenuItem;
    itemRot: TMenuItem;
    itemGruen: TMenuItem;
    itemBlau: TMenuItem;
    itemGrau: TMenuItem;
    Ansicht1: TMenuItem;
    Statusbar2: TMenuItem;
    Info1: TMenuItem;
    berGiraffo1: TMenuItem;
    Hilfe1: TMenuItem;
    ActionList1: TActionList;
    actReset: TAction;
    actZufall: TAction;
    actLoesen: TAction;
    actBeenden: TAction;
    actStatusbar: TAction;
    ImageList1: TImageList;
    procedure FormCreate(Sender: TObject);
    procedure ClosebuttonClick(Sender: TObject);
    procedure AboutButtonClick(Sender: TObject);
    function CheckIfMatching(ElementA: string; ElementB: string): boolean;
    function GetMatchCount(A, B: array of string): Integer;
    procedure btnZufallClick(Sender: TObject);
    procedure outputMemo(AString: string);
    procedure Timer1Timer(Sender: TObject);
    procedure btnMemoLeerenClick(Sender: TObject);
    procedure orderSolved;
    procedure ListBox1DrawItem(Control: TWinControl; Index: Integer;
    Rect: TRect; State: TOwnerDrawState);
    procedure ListBox1Click(Sender: TObject);
    procedure btnItemUpClick(Sender: TObject);
    procedure btnItemDownClick(Sender: TObject);
    procedure btnLoesungClick(Sender: TObject);

    procedure btnResetClick(Sender: TObject);

   procedure updateProgressbar();
    procedure FormShow(Sender: TObject);
    procedure actStatusbarExecute(Sender: TObject);
    procedure Rot1Click(Sender: TObject);
    procedure colorSelection(Sender: TObject);


    private
    { Private declarations }
    procedure UMActivate( var msg: TMessage ); message UM_ACTIVATE;
  public
    { Public declarations }
  end;

var
  Form1: TForm1;
    { Referenz Array }
  MasDef: array [1 .. 5] of string;
  { Aktueller Array sichtbar }
  Mas: array [1 .. 5] of string;
  { Hilfsvariablen }
  selectedItemIndex: Integer;
  topReached, bottomReached: boolean;
  orderOK: boolean;

  sExePath: string;

const
  pathRot = 'Rot\';
  pathGruen = 'Gruen\';
  pathBlau = 'Blau\';
  pathGrau = 'Grau\';

implementation
uses splashscreenU;

{$R *.DFM}

procedure TForm1.FormCreate(Sender: TObject);
var
  I: INteger;
begin
  For i:= 1 to 10 do Begin
    ShowSplashScreenMessage(
      Format( 'Step %d of 10', [i] ));
    Sleep( 200 );
  End;
  PostMessage( handle, UM_ACTIVATE, 0, 0 );
  sExePath := sExePath + pathRot;
  Mas[1] := '01_Kopf.bmp';
  Mas[2] := '02_Hals.bmp';
  Mas[3] := '03_Bauch.bmp';
  Mas[4] := '04_Knie.bmp';
  Mas[5] := '05_Fuesse.bmp';

  MasDef[1] := '01_Kopf.bmp';
  MasDef[2] := '02_Hals.bmp';
  MasDef[3] := '03_Bauch.bmp';
  MasDef[4] := '04_Knie.bmp';
  MasDef[5] := '05_Fuesse.bmp';

  updateProgressbar();

end;

procedure TForm1.FormShow(Sender: TObject);
begin
  Self.Height := Screen.WorkAreaHeight;
end;

procedure TForm1.actStatusbarExecute(Sender: TObject);
begin
   if StatusBar1.Visible then
  begin
    StatusBar1.Visible := false;
  end
  else
  begin
    StatusBar1.Visible := true;
  end;
end;

procedure TForm1.btnItemDownClick(Sender: TObject);
var
  CurrIndex, LastIndex, I, modIndexA, modIndexB: Integer;
  infoString: string;
  m: Integer;
  StrArr: array [1 .. 5] of string;
  StrArrAux: array [1 .. 5] of string;
begin
  with ListBox1 do
  begin
    modIndexA := ItemIndex + 1;
    CurrIndex := ItemIndex;
    LastIndex := Items.Count;
    if ItemIndex <> -1 then
    begin
      if CurrIndex + 1 < LastIndex then
      begin
        Items.Move(ItemIndex, (CurrIndex + 1));
        ItemIndex := CurrIndex + 1;
        modIndexB := ItemIndex + 1;
      end;
    end;
  end;

  for I := 1 to ListBox1.Items.Count do
  begin
    StrArr[I] := ListBox1.Items.Strings[I - 1];
  end;

  for m := 1 to High(Mas) do
  begin
    infoString := '';
    infoString := Mas[m];
    StrArrAux[m] := infoString;
  end;

  Mas[modIndexA] := Mas[modIndexB];
  Mas[modIndexB] := StrArrAux[modIndexA];


  // for m := 1 to High(StrArr) do
  // begin
  // infoString := '';
  // infoString := MasDef[StrToInt(ListBox1.Items.Strings[m - 1])];
  // Mas[m] := infoString;
  // end;

  topReached := ListBox1.ItemIndex = 0;
  bottomReached := ListBox1.ItemIndex = 4;

  outputMemo('Top reached: ' + Format('%s', [BoolToStr(topReached)]));
  outputMemo('Bottom reached: ' + BoolToStr(bottomReached));
  outputMemo('Bottom reached: ' + BoolToStr(ListBox1.Selected
    [ListBox1.ItemIndex]) + ' ItemIndex: ' + IntToStr(ListBox1.ItemIndex));

  if not topReached then
  begin
    btnItemUp.Enabled := true;
  end;

  if (ListBox1.Selected[0]) then
  begin
    btnItemUp.Enabled := false;
  end;

  if (ListBox1.Selected[4]) then
  begin
    btnItemDown.Enabled := false;
  end;


  //
  // for m := 1 to High(Mas) do
  // begin
  // if Mas[m] = MasDef[m] then
  // begin
  // orderOK := true;
  // end
  // else
  // begin
  // orderOK := false;
  // break;
  // end;
  // end;
  //
  // if orderOK then
  // begin
  /// /    ShowMessage('oK');
  // end;

  // Memo1.Lines.Add(IntToStr(GetMatchCount(Mas, MasDef)));
  ListBox1.Repaint;
  updateProgressbar();
end;

procedure TForm1.btnItemUpClick(Sender: TObject);
var
  CurrIndex, modIndexA, modIndexB: Integer;
  I: Integer;
  infoString: string;
  m: Integer;
  StrArr: array [1 .. 5] of string;

  StrArrAux: array [1 .. 5] of string;

begin
  with ListBox1 do
  begin
    if ItemIndex > 0 then
    begin
      modIndexA := ItemIndex + 1;
      CurrIndex := ItemIndex;
      Items.Move(ItemIndex, (CurrIndex - 1));
      ItemIndex := CurrIndex - 1;
      modIndexB := CurrIndex;
    end;
  end;

  for I := 1 to ListBox1.Items.Count do
  begin
    StrArr[I] := ListBox1.Items[I - 1];
  end;

  for m := 1 to High(Mas) do
  begin
    infoString := '';
    infoString := Mas[m];
    StrArrAux[m] := infoString;
  end;

  //

  Mas[modIndexB] := Mas[modIndexA];
  Mas[modIndexA] := StrArrAux[modIndexB];

  // for m := 1 to High(StrArr) do
  // begin
  // infoString := '';
  // infoString := MasDef[StrToInt(ListBox1.Items.Strings[m - 1])];
  // Mas[m] := infoString;
  // end;

  topReached := ListBox1.ItemIndex = 0;
  bottomReached := ListBox1.ItemIndex = 4;

  // Memo1.Lines.Add('Top reached: ' + BoolToStr(topReached));
  // Memo1.Lines.Add('Bottom reached: ' + BoolToStr(bottomReached));
  // Memo1.Lines.Add('Bottom reached: ' + BoolToStr(ListBox1.Selected[ListBox1.ItemIndex]) + 'ItemIndex: ' +IntToStr(ListBox1.ItemIndex));

  if not bottomReached then
  begin
    btnItemDown.Enabled := true;
  end;

  if (ListBox1.Selected[0]) then
  begin
    btnItemUp.Enabled := false;
  end;

  if (ListBox1.Selected[4]) then
  begin
    btnItemDown.Enabled := false;
  end;

  // for m := 1 to High(Mas) do
  // begin
  // if Mas[m] = MasDef[m] then
  // begin
  // orderOK := true;
  // end
  // else
  // begin
  // orderOK := false;
  // break;
  // end;
  // end;

  // if orderOK then
  // begin
  /// /    ShowMessage('oK');
  // end;
  if High(MasDef) = GetMatchCount(Mas, MasDef) then
  begin
    orderOK := true;
    // Memo1.Lines.Add('Alles wieder an der richtigen Stelle!');
  end;

  ListBox1.Repaint;
  updateProgressbar();
end;

procedure TForm1.btnLoesungClick(Sender: TObject);
var
  m: Integer;
begin
  for m := 1 to High(Mas) do
  begin
    Mas[m] := MasDef[m];
  end;
  ListBox1.Repaint;
  updateProgressbar();
end;

procedure TForm1.btnMemoLeerenClick(Sender: TObject);
begin
    memInfo.Clear;
end;

procedure TForm1.btnResetClick(Sender: TObject);
var
  m: Integer;
begin
  for m := 1 to High(Mas) do
  begin
    Mas[m] := MasDef[m];
  end;
  ListBox1.Repaint;
  updateProgressbar();
end;

procedure TForm1.btnZufallClick(Sender: TObject);
Var
  A: Array [1 .. 5] Of Integer;
  I, N: Integer;
  Auxiliar: Integer;
  K: Integer;
  infoString: string;
Begin
  Randomize;
  For I := 1 To 5 Do
  Begin
    A[I] := I; // First put them in order
  End;
  For I := 1 To 5 Do
  Begin // Now do permutations, so re-order in a random order
    N := Random(5) + 1;
    Auxiliar := A[1];
    A[1] := A[N];
    A[N] := Auxiliar;
  End;

  for K := 1 to High(A) do
  begin
    // Memo1.Lines.Add(IntToStr(A[K]));
    infoString := '';
    infoString := MasDef[A[K]];
    Mas[K] := infoString;
  end;

  ListBox1.Repaint;

  for K := 0 to ListBox1.Count - 1 do
  begin
    outputMemo('Index: ' + ListBox1.Items[K] + ': ' + Mas[K + 1]);
  end;

  for K := 0 to ListBox1.Count - 1 do
  begin
    outputMemo(ListBox1.Items[K]);
  end;

  updateProgressbar();
end;

function TForm1.CheckIfMatching(ElementA, ElementB: string): boolean;
begin
  Result := false;
  if ElementA = ElementB then
  begin
    Result := true;
  end;
end;

function TForm1.GetMatchCount(A, B: array of string): Integer;
var
  I: Integer;
begin
  Result := 0;
  for I := low(A) to high(A) do
    if CheckIfMatching(A[I], B[I]) = true then
      inc(Result);
end;

procedure TForm1.ListBox1Click(Sender: TObject);
begin
  selectedItemIndex := -1;
  selectedItemIndex := ListBox1.ItemIndex;
  // Memo1.Lines.Add('ItemIndex: ' + IntToStr(ListBox1.ItemIndex) + ' ' + Mas[ListBox1.ItemIndex + 1] + #13#10);

  // Memo1.Lines.Add('Text: ' + ListBox1.Items[ListBox1.ItemIndex]);
  // for K := 0 to ListBox1.Count-1 do
  // begin
  // Memo1.Lines.Add('Listbox: ' + #13#10 + 'ItemIndex: ' + ListBox1.Items[K] + mas[k+1] + #13#10);
  // end;

  if (ListBox1.ItemIndex = 0) or (ListBox1.Selected[0]) then
  begin
    btnItemUp.Enabled := false;
  end
  else
  begin
    btnItemUp.Enabled := true;
  end;

  if (ListBox1.ItemIndex = 4) or (ListBox1.Selected[4]) then
  begin
    btnItemDown.Enabled := false;
  end
  else
  begin
    btnItemDown.Enabled := true;
  end;

  // JvGradientProgressBar1.Position := GetMatchCount(Mas, MasDef);
  updateProgressbar();
end;

procedure TForm1.ListBox1DrawItem(Control: TWinControl; Index: Integer;
  Rect: TRect; State: TOwnerDrawState);
var
  Bitmap: TBitmap;
  R: TRect;
begin

  with (Control as TListBox).Canvas do
  begin
    Bitmap := TBitmap.Create;
    FillRect(Rect);
    Bitmap.LoadFromFile(sExePath + Mas[Index + 1]);
    if Bitmap <> nil then
    begin
      R := Bounds(Rect.Left + 2, Rect.Top, Rect.Bottom - Rect.Top,
        Rect.Bottom - Rect.Top);
      StretchDraw(R, Bitmap);
    end;
    // TextOut(Rect.Left + 210, Rect.Top, Mas[Index + 1]);
    Bitmap.Free;
  end;
end;

procedure TForm1.orderSolved;
begin
  Mas[1] := '01_Kopf.bmp';
  Mas[2] := '02_Hals.bmp';
  Mas[3] := '03_Bauch.bmp';
  Mas[4] := '04_Knie.bmp';
  Mas[5] := '05_Fuesse.bmp';
end;

procedure TForm1.outputMemo(AString: string);
begin
  memInfo.Lines.Add(AString);
end;

procedure TForm1.Rot1Click(Sender: TObject);
var
  sExePath: string;
begin

 ShowMessage(sExePath);
end;

procedure TForm1.Timer1Timer(Sender: TObject);
var
  hr, min, sec, msec: Word;
begin
  DecodeTime(now, hr, min, sec, msec);
  StatusBar1.Panels[0].Text := FormatDateTime('hh:nn:ss', now);
  StatusBar1.Panels[1].Text := DateToStr(now);
end;

procedure TForm1.ClosebuttonClick(Sender: TObject);
begin
  close;
end;

procedure TForm1.colorSelection(Sender: TObject);
begin
 sExePath := ExtractFilePath(Application.ExeName);
 with Sender as TMenuItem do
  begin
    case Tag of
      0:  sExePath := sExePath + pathRot;
      1:  sExePath := sExePath + pathGruen;
      2:  sExePath := sExePath + pathBlau;
      3:  sExePath := sExePath + pathGrau;
    end;
  end;
  ListBox1.Repaint;
  updateProgressbar();
end;

procedure TForm1.UMActivate(var msg: TMessage);
begin
  { HACK ALERT! Without this the main form will come up not active
    after the splash screen has been destroyed. }
  SetForegroundWindow( handle );
end;

procedure TForm1.updateProgressbar;
begin
  JvGradientProgressBar1.Position := GetMatchCount(Mas, MasDef);
  lblTrefferCount.Caption :=
    IntToStr( High(Mas) - ( High(Mas) - GetMatchCount(Mas, MasDef)));
  lblInsgesamtCount.Caption := IntToStr( High(Mas));
end;

procedure TForm1.AboutButtonClick(Sender: TObject);
begin
  With TSplashform.Create( nil ) Do
  Try
    Showmodal;
  Finally
    Free;
  End; { Finally }
end;

end.
